package cn.thoughtworks.school.programCenter.entities.vo;

import cn.thoughtworks.school.programCenter.entities.Program;
import cn.thoughtworks.school.programCenter.entities.Task;
import lombok.*;

import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ProgramAssignmentsVo {
    private Program program;
    private List<Task> tasks;

    public static List<ProgramAssignmentsVo> build(List<Program> programs, List<Task> tasks) {
        List<Task> filteredTasks = tasks.stream()
            .filter(ProgramAssignmentsVo::hasBasicQuizAssignment)
            .collect(Collectors.toList());

        return programs.stream()
            .map(program -> ProgramAssignmentsVo.builder()
                .program(program)
                .tasks(filteredTasks.stream().filter(task -> task.getProgramId().equals(program.getId())).collect(Collectors.toList()))
                .build())
            .filter(item -> !item.getTasks().isEmpty())
            .collect(Collectors.toList());

    }

    private static boolean hasBasicQuizAssignment(Task task) {
        return task.getAssignments().stream().anyMatch(assignment -> assignment.getType().equals("BASIC_QUIZ"));
    }
}
